﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.CUiRenameToolWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.RenameTool.RulesManager;
using EMPAPPBASELib;
using EMPAPPSERVICESLib;
using EMPCLIENTCOMMANDSLib;
using EMPTYPELIBRARYLib;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPF;


namespace DnProcessSimulateCommands.RenameTool;

public class CUiRenameToolWindow : TxWindow, INotifyPropertyChanged, IComponentConnector
{
  private CApRenameToolManager manager;
  private CApRulesSetManager rulesManager;
  private List<RuleBase> rules = new List<RuleBase>();
  private List<string> objectNames;
  private bool _directChildrenListNeedsUpdate;
  private bool _allChildrenListNeedsUpdate;
  private bool clearTheRulesList = true;
  private ObservableCollection<string> comboboxItemsSource = new ObservableCollection<string>();
  private List<string> filteredOutObjects = new List<string>();
  private const string RULE_ORDER_SPEARATOR = ".";
  internal CUiRenameToolWindow RenameWindow;
  internal System.Windows.Controls.Button btApply;
  internal System.Windows.Controls.Button button2;
  internal System.Windows.Controls.ComboBox cbScopeBox;
  internal ComboBoxItem cbi1;
  internal ComboBoxItem cbi2;
  internal ComboBoxItem cbi3;
  internal ToggleButton btnFilter;
  internal System.Windows.Controls.DataGrid m_flexGrid;
  public TxSplitButton btnSplitRemoveScope;
  internal System.Windows.Controls.Button btAdd;
  internal System.Windows.Controls.Button btUp;
  internal System.Windows.Controls.Image imgArrowUpTop;
  internal System.Windows.Controls.Button btDown;
  internal System.Windows.Controls.Image imgArrowDownTop;
  internal System.Windows.Controls.ListBox listOfRules;
  public TxSplitButton btnSplitRemoveRule;
  public TxSplitButton btnSplitAddRule;
  internal System.Windows.Controls.Button btnEditRule;
  internal System.Windows.Controls.Button btnRuleSet;
  internal System.Windows.Controls.Image imgRule;
  public TxSplitButton btnSplitSave;
  internal System.Windows.Controls.ComboBox cbRules;
  internal System.Windows.Controls.Button btnRulesUp;
  internal System.Windows.Controls.Image imgArrowUpBottom;
  internal System.Windows.Controls.Button btnRulesDown;
  internal System.Windows.Controls.Image imgArrowDownBottom;
  private bool _contentLoaded;

  public CUiRenameToolWindow()
  {
    this.manager = CApRenameToolManager.GetInstance();
    this.rulesManager = CApRulesSetManager.Instance;
    this.manager.ApplyScope = ApplyScope.Parents;
    this.manager.Parents.CollectionChanged += new NotifyCollectionChangedEventHandler(this.Parents_CollectionChanged);
    this.InitializeComponent();
    ImageSource[] imageSourceArray = new ImageSource[2]
    {
      TxImageManager.GetImageByKey("Save").ConvertTo<ImageSource>(new System.Drawing.Size()),
      TxImageManager.GetImageByKey("SaveAs").ConvertTo<ImageSource>(new System.Drawing.Size())
    };
    this.btnSplitSave.Icon = imageSourceArray[0];
    this.btnSplitSave.DropDownList[0].Icon = imageSourceArray[0];
    this.btnSplitSave.DropDownList[1].Icon = imageSourceArray[1];
    this.imgRule.Source = TxImageManager.GetImageByKey("RulesManager").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.imgArrowUpTop.Source = TxImageManager.GetImageByKey("ArrowUp").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.imgArrowDownTop.Source = TxImageManager.GetImageByKey("ArrowDown").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.imgArrowUpBottom.Source = TxImageManager.GetImageByKey("ArrowUp").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.imgArrowDownBottom.Source = TxImageManager.GetImageByKey("ArrowDown").ConvertTo<ImageSource>(new System.Drawing.Size());
    this.btUp.IsEnabled = this.btDown.IsEnabled = false;
    this.m_flexGrid.SelectionChanged += new SelectionChangedEventHandler(this.m_flexGrid_SelectionChanged);
    this.rulesManager.SetSerializableTypes(this.manager.GetPluginTypes());
    ((FrameworkElement) this).DataContext = (object) this.manager.GetDataElements;
    this.AddSelectedItems();
    this.objectNames = new List<string>();
    CApDataElements.ItemWasChecked += new D(this.CApDataElements_ItemWasChecked);
    CApRulesSetManager.Instance.DeSerializeRenameRuleSet();
    this.InitializeCombobox();
    this.SetButtons();
    // ISSUE: explicit non-virtual call
    __nonvirtual (((Window) this).Title) = XamlResourceManager.GetString("RENAME_TOOL_TITLE");
  }

  private void Parents_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  {
    this._directChildrenListNeedsUpdate = true;
    this._allChildrenListNeedsUpdate = true;
  }

  private void m_flexGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    this.RefreshUpDownButtonsState();
  }

  private void CApDataElements_ItemWasChecked()
  {
    this.UpdatePreview();
    this.SetButtons();
  }

  private void cancel_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void btApply_Click(object sender, RoutedEventArgs e)
  {
    System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    try
    {
      if (!this.manager.IsRulesStateSuccess && TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_ERROR_ON_APPLY"), XamlResourceManager.GetString("RENAME_TOOL_WARNING"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        return;
      List<EmpObjectKey> empObjectKeyList = new List<EmpObjectKey>();
      List<string> stringList = new List<string>();
      bool flag = false;
      if (this.manager.IsRunningInDesigner())
      {
        int index1 = -1;
        for (int index2 = 0; index2 < this.manager.Elements.Count; ++index2)
        {
          if (this.manager.Elements[index2].IsChecked)
          {
            ++index1;
            if (index1 < this.objectNames.Count)
              flag |= this.objectNames[index1].Equals("");
            empObjectKeyList.Add(this.manager.Elements[index2].Key);
          }
        }
        if (this.CheckOutObjects(ref empObjectKeyList))
        {
          if (flag && TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_EMPTY_NAMES_CONTINUE"), XamlResourceManager.GetString("RENAME_TOOL_LABEL_RENAME_TOOL"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            return;
          this.CheckRuleMessageResult(this.manager.SetObjectsCaption(ref empObjectKeyList, this.objectNames), true);
          this.manager.RefreshObjectNames(ref empObjectKeyList, this.objectNames);
        }
        else
        {
          int num = (int) TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_CO_BY_OTHERS"), XamlResourceManager.GetString("RENAME_TOOL_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
      }
      else
      {
        int index3 = -1;
        List<ITxObject> keys = new List<ITxObject>();
        for (int index4 = 0; index4 < this.manager.Elements.Count; ++index4)
        {
          if (this.manager.Elements[index4].IsChecked)
          {
            ++index3;
            if (index3 < this.objectNames.Count)
              flag |= this.objectNames[index3].Equals("");
            keys.Add(this.manager.Elements[index4].Obj);
          }
        }
        if (flag && TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_EMPTY_NAMES_CONTINUE"), XamlResourceManager.GetString("RENAME_TOOL_LABEL_RENAME_TOOL"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
          return;
        this.CheckRuleMessageResult(this.manager.SetObjectsCaption(ref keys, this.objectNames), true);
        this.manager.RefreshObjectNames(ref keys, this.objectNames);
      }
    }
    finally
    {
      System.Windows.Forms.Cursor.Current = current;
    }
    this.UpdatePreview();
  }

  private void m_flexGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    switch (e.Key)
    {
      case Key.Space:
        this.CheckSelectedRows();
        break;
      case Key.Delete:
        this.RemoveObjects();
        break;
    }
  }

  private void InitializeCombobox()
  {
    this.comboboxItemsSource.Clear();
    this.comboboxItemsSource.Add("");
    foreach (CApRulesSet ruleSet in (Collection<CApRulesSet>) CApRulesSetManager.Instance.RuleSets)
      this.comboboxItemsSource.Add(ruleSet.Name);
    this.cbRules.ItemsSource = (IEnumerable) this.comboboxItemsSource;
    this.cbRules.SelectedIndex = 0;
  }

  public void UpdatePreview()
  {
    if (this.manager.Elements.Count > 0 && this.m_flexGrid.SelectedIndex == -1)
      this.m_flexGrid.SelectedIndex = 0;
    this.objectNames.Clear();
    foreach (CApRenameElement element in (Collection<CApRenameElement>) this.manager.Elements)
    {
      if (element.IsChecked)
        this.objectNames.Add(element.Name);
    }
    this.ExecuteRules(this.objectNames);
    int num = 0;
    foreach (CApRenameElement element in (Collection<CApRenameElement>) this.manager.Elements)
    {
      if (element.IsChecked)
        element.Preview = this.objectNames[num++];
    }
  }

  private void RefreshUpDownButtonsState()
  {
    if (this.m_flexGrid.Items.Count == 0)
    {
      this.btUp.IsEnabled = this.btDown.IsEnabled = false;
    }
    else
    {
      bool flag1 = true;
      bool flag2 = true;
      foreach (CApRenameElement capRenameElement in this.m_flexGrid.SelectedItems.Cast<CApRenameElement>().ToList<CApRenameElement>())
      {
        if (this.manager.Elements.IndexOf(capRenameElement) == 0)
          flag1 = false;
        if (this.manager.Elements.IndexOf(capRenameElement) == this.manager.Elements.Count - 1)
          flag2 = false;
      }
      if (this.btUp.IsEnabled != flag1)
        this.btUp.IsEnabled = flag1;
      if (this.btDown.IsEnabled == flag2)
        return;
      this.btDown.IsEnabled = flag2;
    }
  }

  private void SetButtons()
  {
    if (this.manager.Elements.Count > 0 && this.rules.Count > 0)
    {
      bool flag = false;
      foreach (CApRenameElement element in (Collection<CApRenameElement>) this.manager.Elements)
      {
        if (element.IsChecked)
        {
          flag = true;
          break;
        }
      }
      this.btApply.IsEnabled = flag;
    }
    else
      this.btApply.IsEnabled = false;
    if (this.manager.ApplyScope == ApplyScope.DirectChildren || this.manager.ApplyScope == ApplyScope.AllChildren)
    {
      this.btAdd.IsEnabled = false;
      ((UIElement) this.btnSplitRemoveScope).IsEnabled = false;
    }
    else
    {
      this.btAdd.IsEnabled = true;
      if (this.manager.Elements.Count > 0)
        ((UIElement) this.btnSplitRemoveScope).IsEnabled = true;
      else
        ((UIElement) this.btnSplitRemoveScope).IsEnabled = false;
    }
    if (this.listOfRules.Items.Count > 0)
    {
      ((UIElement) this.btnSplitRemoveRule).IsEnabled = true;
      ((UIElement) this.btnSplitSave).IsEnabled = true;
      if (this.listOfRules.SelectedIndex > -1)
      {
        if (this.listOfRules.SelectedItems.Count == 1)
          this.btnEditRule.IsEnabled = true;
        else
          this.btnEditRule.IsEnabled = false;
      }
      else
      {
        if (this.btnSplitRemoveRule.SelectedItem == 0)
          ((UIElement) this.btnSplitRemoveRule).IsEnabled = false;
        this.btnEditRule.IsEnabled = false;
      }
    }
    else
    {
      this.btnEditRule.IsEnabled = false;
      ((UIElement) this.btnSplitRemoveRule).IsEnabled = false;
      ((UIElement) this.btnSplitSave).IsEnabled = false;
    }
    bool flag1 = true;
    bool flag2 = true;
    if (this.listOfRules.SelectedItems.Count == 0)
    {
      this.btnRulesUp.IsEnabled = this.btnRulesDown.IsEnabled = false;
    }
    else
    {
      foreach (object selectedItem in (IEnumerable) this.listOfRules.SelectedItems)
      {
        if (this.listOfRules.Items.IndexOf(selectedItem) == 0)
          flag1 = false;
        if (this.listOfRules.Items.IndexOf(selectedItem) == this.listOfRules.Items.Count - 1)
          flag2 = false;
      }
      if (this.btnRulesUp.IsEnabled != flag1)
        this.btnRulesUp.IsEnabled = flag1;
      if (this.btnRulesDown.IsEnabled == flag2)
        return;
      this.btnRulesDown.IsEnabled = flag2;
    }
  }

  public bool CheckOutObjects(ref List<EmpObjectKey> objectsList)
  {
    bool flag = false;
    EmpObjectKey[] array = objectsList.ToArray<EmpObjectKey>();
    IEmpNodesServices iempNodesServices = (IEmpNodesServices) new EmpNodesServicesClass();
    if (objectsList.Count > 0)
    {
      string[] strArray = new string[1]{ "Ex_cioState" };
      EmpContext empContext;
      empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
      foreach (EmpFieldInfo empFieldInfo in iempNodesServices.GetFieldsEx(ref empContext, ref array, ref strArray))
      {
        EmpEnumCOState empEnumCoState = (EmpEnumCOState) empFieldInfo.Value;
        if (empEnumCoState.Equals((object) (EmpEnumCOState) 2))
          return false;
        if (empEnumCoState.Equals((object) (EmpEnumCOState) 0))
          flag = true;
      }
      if (flag)
      {
        if (TxMessageBox.Show(XamlResourceManager.GetString("RENAME_TOOL_CI"), XamlResourceManager.GetString("RENAME_TOOL_CI_OBJECTS"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
          return false;
        this.executeCO(ref array);
      }
    }
    return true;
  }

  private void executeCO(ref EmpObjectKey[] nodeVec)
  {
    if (nodeVec.Length == 0)
      return;
    IEmpCmd iempCmd = (IEmpCmd) new EmpCheckOutCmdClass();
    IEmpCheckOutCmdParams checkOutCmdParams = (IEmpCheckOutCmdParams) new EmpCheckOutCmdParamsClass();
    checkOutCmdParams.Selection = this.CreateEmpAppItemList(nodeVec);
    checkOutCmdParams.ShowDialog = false;
    checkOutCmdParams.Recursive = false;
    iempCmd.Execute((IEmpCmdParams) checkOutCmdParams);
  }

  private EmpAppItemList CreateEmpAppItemList(EmpObjectKey[] list)
  {
    EmpAppItemList empAppItemList = (EmpAppItemList) new EmpAppItemListClass();
    foreach (EmpObjectKey empObjectKey1 in list)
    {
      EmpAppItem empAppItem = (EmpAppItem) new EmpAppItemClass();
      EmpObjectKey empObjectKey2;
      empObjectKey2.objectId = empObjectKey1.objectId;
      ref ((IEmpAppItem) empAppItem).Key = ref empObjectKey2;
      ((IEmpAppItemList) empAppItemList).Add(empAppItem);
    }
    return empAppItemList;
  }

  private void ExecuteRules(List<string> names)
  {
    this.manager.IsRulesStateSuccess = true;
    for (int index = 0; index < this.rules.Count; ++index)
    {
      RuleMessage ruleMessage = this.rules[index].Execute(names);
      if (!ruleMessage.Success)
      {
        int num = (int) TxMessageBox.Show(ruleMessage.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.None);
        this.manager.IsRulesStateSuccess = false;
        break;
      }
      ListBoxItem listBoxItem = this.listOfRules.Items[index] as ListBoxItem;
      switch (this.rules[index].State)
      {
        case RuleState.Success:
          listBoxItem.ToolTip = (object) null;
          break;
        case RuleState.Error:
          listBoxItem.Foreground = (System.Windows.Media.Brush) System.Windows.Media.Brushes.Red;
          System.Windows.Controls.ToolTip toolTip = new System.Windows.Controls.ToolTip();
          toolTip.Content = (object) XamlResourceManager.GetString("RENAME_TOOL_RULE_WARNING_TOOLTIP");
          listBoxItem.ToolTip = (object) toolTip;
          this.manager.IsRulesStateSuccess = false;
          break;
      }
      this.listOfRules.Items.Refresh();
    }
  }

  private void CheckSelectedRows()
  {
    if (this.m_flexGrid.SelectedIndex == -1)
      return;
    foreach (CApRenameElement capRenameElement in this.m_flexGrid.SelectedItems.Cast<CApRenameElement>().ToList<CApRenameElement>())
      capRenameElement.IsChecked = !capRenameElement.IsChecked;
  }

  private void btRemove_Click(object sender, RoutedEventArgs e)
  {
    if (this.btnSplitRemoveScope.SelectedItem == 0)
      this.RemoveObjects();
    else
      this.RemoveAllObjects();
  }

  private void RemoveObjects()
  {
    if (this.manager.ApplyScope == ApplyScope.DirectChildren || this.manager.ApplyScope == ApplyScope.AllChildren || this.m_flexGrid.SelectedIndex == -1)
      return;
    foreach (CApRenameElement capRenameElement in this.m_flexGrid.SelectedItems.Cast<CApRenameElement>().ToList<CApRenameElement>())
    {
      this.manager.Parents.RemoveAt(this.manager.Parents.IndexOf(capRenameElement));
      this.manager.Elements.RemoveAt(this.manager.Elements.IndexOf(capRenameElement));
    }
    this.UpdatePreview();
    this.SetButtons();
  }

  private void ClearAll()
  {
    this.manager.Clear();
    this.ClearPreview();
    this.UpdatePreview();
    this.SetButtons();
  }

  private void RemoveAllObjects()
  {
    this.manager.Clear();
    this.ClearPreview();
    this.UpdatePreview();
    this.SetButtons();
  }

  private void RemoveAllRules()
  {
    this.rules.Clear();
    this.listOfRules.Items.Clear();
    this.ClearPreview();
    this.SetButtons();
  }

  private void add_Click(object sender, RoutedEventArgs e)
  {
    this.AddSelectedItems();
    this.UpdatePreview();
    this.SetButtons();
    this.RefreshUpDownButtonsState();
    this.m_flexGrid.Focus();
  }

  private void btUp_Click(object sender, RoutedEventArgs e)
  {
    int selectedIndex1 = this.m_flexGrid.SelectedIndex;
    List<CApRenameElement> list = this.m_flexGrid.SelectedItems.Cast<CApRenameElement>().ToList<CApRenameElement>();
    List<int> intList = new List<int>();
    int selectedIndex2 = this.m_flexGrid.SelectedIndex;
    if (list.Count != this.m_flexGrid.Items.Count)
    {
      foreach (CApRenameElement capRenameElement in list)
      {
        if (this.manager.Elements.IndexOf(capRenameElement) == 0)
          return;
        intList.Add(this.manager.Elements.IndexOf(capRenameElement));
      }
      list.Clear();
      foreach (int index in intList)
      {
        if (index > 0)
        {
          CApRenameElement element = this.manager.Elements[index];
          this.manager.Elements.RemoveAt(index);
          this.manager.Elements.Insert(index - 1, element);
        }
      }
      this.m_flexGrid.SelectedIndex = selectedIndex2 - 1;
    }
    this.UpdatePreview();
  }

  private void btDown_Click(object sender, RoutedEventArgs e)
  {
    List<CApRenameElement> list = this.m_flexGrid.SelectedItems.Cast<CApRenameElement>().ToList<CApRenameElement>();
    List<int> intList = new List<int>();
    int selectedIndex = this.m_flexGrid.SelectedIndex;
    if (list.Count != this.m_flexGrid.Items.Count)
    {
      list.Clear();
      for (int index = intList.Count - 1; index > -1; --index)
      {
        if (intList[index] < this.manager.Elements.Count - 1)
        {
          CApRenameElement element = this.manager.Elements[intList[index]];
          this.manager.Elements.RemoveAt(intList[index]);
          this.manager.Elements.Insert(intList[index] + 1, element);
        }
      }
      this.m_flexGrid.SelectedIndex = selectedIndex + 1;
      foreach (int num in intList)
        (this.m_flexGrid.SelectedItems[num + 1] as DataGridRow).IsSelected = true;
    }
    this.UpdatePreview();
  }

  private void ScopeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (!((FrameworkElement) this).IsInitialized)
      return;
    System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    this.manager.Elements.Clear();
    try
    {
      List<CApRenameElement> capRenameElementList = (List<CApRenameElement>) null;
      switch (this.cbScopeBox.SelectedIndex)
      {
        case 0:
          this.manager.ApplyScope = ApplyScope.Parents;
          capRenameElementList = this.manager.Parents.ToList<CApRenameElement>();
          break;
        case 1:
          this.manager.ApplyScope = ApplyScope.DirectChildren;
          if (this._directChildrenListNeedsUpdate)
            this.CheckRuleMessageResult(this.manager.AddChildren(this.manager.Parents.ToList<CApRenameElement>(), true), true);
          else
            capRenameElementList = this.manager.DirectChildren;
          this._directChildrenListNeedsUpdate = false;
          break;
        case 2:
          this.manager.ApplyScope = ApplyScope.AllChildren;
          if (this._allChildrenListNeedsUpdate)
            this.CheckRuleMessageResult(this.manager.AddChildren(this.manager.Parents.ToList<CApRenameElement>(), false), true);
          else
            capRenameElementList = this.manager.AllChildren;
          this._allChildrenListNeedsUpdate = false;
          break;
      }
      if (capRenameElementList != null)
      {
        foreach (CApRenameElement capRenameElement in capRenameElementList)
          this.manager.GetDataElements.AddRootNode(capRenameElement);
      }
      this.ResetFilter();
      this.UpdatePreview();
      this.SetButtons();
    }
    finally
    {
      System.Windows.Forms.Cursor.Current = current;
    }
  }

  protected void NotifyPropertyChanged(string info)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(info));
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void btRulesUp_Click(object sender, RoutedEventArgs e)
  {
    if (this.listOfRules.Items.Count == 0 || this.listOfRules.SelectedIndex == -1)
      return;
    SortedSet<int> sortedSet = new SortedSet<int>();
    foreach (object selectedItem in (IEnumerable) this.listOfRules.SelectedItems)
    {
      int num = this.listOfRules.Items.IndexOf(selectedItem);
      if (num == 0)
        return;
      sortedSet.Add(num);
    }
    foreach (int num in sortedSet)
    {
      ListBoxItem insertItem = this.listOfRules.Items[num] as ListBoxItem;
      string content1 = insertItem.Content as string;
      int startIndex1 = content1.IndexOf(".");
      insertItem.Content = (object) (num.ToString() + content1.Substring(startIndex1));
      this.listOfRules.Items.RemoveAt(num);
      this.listOfRules.Items.Insert(num - 1, (object) insertItem);
      ListBoxItem listBoxItem = this.listOfRules.Items[num] as ListBoxItem;
      string content2 = listBoxItem.Content as string;
      int startIndex2 = content2.IndexOf(".");
      listBoxItem.Content = (object) ((num + 1).ToString() + content2.Substring(startIndex2));
      RuleBase rule = this.rules[num];
      this.rules.RemoveAt(num);
      this.rules.Insert(num - 1, rule);
    }
    foreach (int num in sortedSet)
      (this.listOfRules.Items[num - 1] as ListBoxItem).IsSelected = true;
    this.UpdatePreview();
  }

  private void btRulesDown_Click(object sender, RoutedEventArgs e)
  {
    if (this.listOfRules.Items.Count == 0 || this.listOfRules.SelectedIndex == -1)
      return;
    SortedSet<int> sortedSet = new SortedSet<int>();
    foreach (object selectedItem in (IEnumerable) this.listOfRules.SelectedItems)
    {
      int num = this.listOfRules.Items.IndexOf(selectedItem);
      if (num == this.listOfRules.Items.Count - 1)
        return;
      sortedSet.Add(num);
    }
    foreach (int num in sortedSet.Reverse())
    {
      ListBoxItem insertItem = this.listOfRules.Items[num] as ListBoxItem;
      string content1 = insertItem.Content as string;
      int startIndex1 = content1.IndexOf(".");
      insertItem.Content = (object) ((num + 2).ToString() + content1.Substring(startIndex1));
      this.listOfRules.Items.RemoveAt(num);
      this.listOfRules.Items.Insert(num + 1, (object) insertItem);
      ListBoxItem listBoxItem = this.listOfRules.Items[num] as ListBoxItem;
      string content2 = listBoxItem.Content as string;
      int startIndex2 = content2.IndexOf(".");
      listBoxItem.Content = (object) ((num + 1).ToString() + content2.Substring(startIndex2));
      RuleBase rule = this.rules[num];
      this.rules.RemoveAt(num);
      this.rules.Insert(num + 1, rule);
    }
    foreach (int num in sortedSet)
      (this.listOfRules.Items[num + 1] as ListBoxItem).IsSelected = true;
    this.UpdatePreview();
  }

  private void btnAddRule_Click()
  {
    RuleBase plugin = this.manager.Plugins[this.btnSplitAddRule.SelectedItem];
    bool? nullable = plugin.ShowWindow((Window) this);
    if (!nullable.HasValue || !nullable.Value)
      return;
    this.rules.Add(plugin);
    ListBoxItem newItem = new ListBoxItem();
    newItem.Content = (object) $"{this.listOfRules.Items.Count + 1}{"."} {plugin.DisplayString}";
    newItem.MouseDoubleClick += new MouseButtonEventHandler(this.newItem_MouseDoubleClick);
    this.listOfRules.Items.Add((object) newItem);
    this.UpdatePreview();
    this.SetButtons();
  }

  private void newItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) => this.EditRule();

  private void btnEditRule_Click(object sender, RoutedEventArgs e) => this.EditRule();

  private void listOfRules_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    this.SetButtons();
  }

  private void EditRule()
  {
    int selectedIndex = this.listOfRules.SelectedIndex;
    if (selectedIndex <= -1)
      return;
    RuleBase rule = this.rules[selectedIndex];
    bool? nullable = rule.ShowWindow((Window) this, true);
    if (!nullable.HasValue || !nullable.Value)
      return;
    (this.listOfRules.Items[selectedIndex] as ListBoxItem).Content = (object) $"{selectedIndex + 1}{"."} {rule.DisplayString}";
    this.listOfRules.Items.Refresh();
    this.listOfRules.SelectedIndex = selectedIndex;
    this.UpdatePreview();
    this.SetButtons();
  }

  private void listOfRules_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (e.Key != Key.Delete)
      return;
    this.RemoveSelectedRules();
  }

  private void btnRemoveRule_Click()
  {
    if (this.btnSplitRemoveRule.SelectedItem == 0)
      this.RemoveSelectedRules();
    else
      this.RemoveAllRules();
  }

  private void RemoveSelectedRules()
  {
    if (this.listOfRules.SelectedIndex == -1)
      return;
    List<int> intList = new List<int>();
    for (int index = this.listOfRules.Items.Count - 1; index >= 0; --index)
    {
      if ((this.listOfRules.Items[index] as ListBoxItem).IsSelected)
        intList.Add(index);
    }
    int num1 = this.listOfRules.Items.Count;
    foreach (int num2 in intList)
    {
      this.listOfRules.Items.RemoveAt(num2);
      this.rules.RemoveAt(num2);
      if (num2 < num1)
        num1 = num2;
    }
    for (int index = num1; index < this.listOfRules.Items.Count; ++index)
    {
      ListBoxItem listBoxItem = this.listOfRules.Items[index] as ListBoxItem;
      string content = listBoxItem.Content as string;
      int startIndex = content.IndexOf(".");
      listBoxItem.Content = (object) ((index + 1).ToString() + content.Substring(startIndex));
    }
    if (this.listOfRules.Items.Count == 0)
      this.ClearPreview();
    else
      this.UpdatePreview();
    this.SetButtons();
  }

  private void btnRemoveAllRules_Click(object sender, RoutedEventArgs e) => this.RemoveAllRules();

  private void btnEditRuleSet_Click(object sender, RoutedEventArgs e)
  {
    int selectedIndex = this.cbRules.SelectedIndex;
    if (selectedIndex == 0)
      this.clearTheRulesList = false;
    List<RuleBase> ruleBaseList = new List<RuleBase>();
    CApRulesSet capRulesSet = new CApRulesSet();
    Guid guid;
    List<RuleBase> list;
    if (selectedIndex < 1)
    {
      guid = Guid.Empty;
      list = this.rules.ToList<RuleBase>();
    }
    else
    {
      guid = this.rulesManager.RuleSets[selectedIndex - 1].UniqueId;
      list = this.rulesManager.RuleSets[selectedIndex - 1].Rules.ToList<RuleBase>();
    }
    CUiRulesSetManager cuiRulesSetManager = new CUiRulesSetManager((Window) this);
    cuiRulesSetManager.InitializeWindow();
    ((Window) cuiRulesSetManager).ShowDialog();
    if (cuiRulesSetManager.DialogWasCanceled && selectedIndex > 0)
    {
      this.rulesManager.RuleSets[selectedIndex - 1].Rules.Clear();
      this.rulesManager.RuleSets[selectedIndex - 1].Rules.AddRange((IEnumerable<RuleBase>) list);
    }
    this.InitializeCombobox();
    int num = 1;
    foreach (CApRulesSet ruleSet in (Collection<CApRulesSet>) this.rulesManager.RuleSets)
    {
      if (ruleSet.UniqueId == guid)
      {
        this.cbRules.SelectedIndex = num;
        return;
      }
      ++num;
    }
    this.cbRules.SelectedIndex = 0;
    this.clearTheRulesList = true;
  }

  private void SaveRule()
  {
    if (this.rules.Count == 0)
      return;
    if (this.cbRules.SelectedIndex == 0)
    {
      this.SaveAsRule();
    }
    else
    {
      if (this.cbRules.SelectedIndex < 0 || this.rulesManager.RuleSets.Count < this.cbRules.SelectedIndex - 1)
        return;
      this.rulesManager.RuleSets[this.cbRules.SelectedIndex - 1].Rules = new List<RuleBase>((IEnumerable<RuleBase>) this.rules);
      this.rulesManager.SerializeRenameRuleSet();
    }
  }

  private void SaveAsRule()
  {
    if (this.rules.Count == 0)
      return;
    CUiSaveRuleWindow cuiSaveRuleWindow = new CUiSaveRuleWindow((Window) this);
    bool? nullable1 = ((Window) cuiSaveRuleWindow).ShowDialog();
    List<RuleBase> rules = new List<RuleBase>((IEnumerable<RuleBase>) this.rules);
    bool? nullable2 = nullable1;
    bool flag = true;
    if (!(nullable2.GetValueOrDefault() == flag & nullable2.HasValue))
      return;
    Guid guid = CApRulesSetManager.Instance.AddRuleSet(cuiSaveRuleWindow.RuleName, cuiSaveRuleWindow.RuleDescription, Guid.NewGuid(), rules);
    int num1 = -1;
    CApRulesSetManager.Instance.SerializeRenameRuleSet();
    this.comboboxItemsSource.Clear();
    this.comboboxItemsSource.Add("");
    int num2 = 0;
    foreach (CApRulesSet ruleSet in (Collection<CApRulesSet>) CApRulesSetManager.Instance.RuleSets)
    {
      ++num2;
      this.comboboxItemsSource.Add(ruleSet.Name);
      if (guid == ruleSet.UniqueId)
        num1 = num2;
    }
    if (num1 == -1)
      return;
    this.cbRules.SelectedIndex = num1;
  }

  private void btnSaveRule_Click(object sender, RoutedEventArgs e)
  {
    if (this.btnSplitSave.SelectedItem == 0)
      this.SaveRule();
    else
      this.SaveAsRule();
  }

  private void cbRules_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (this.cbRules.SelectedIndex == -1)
      return;
    if (this.cbRules.SelectedIndex == 0)
    {
      if (!this.clearTheRulesList)
        return;
      this.listOfRules.Items.Clear();
      this.rules.Clear();
      this.ClearPreview();
      this.SetButtons();
    }
    else
    {
      int index = this.cbRules.SelectedIndex - 1;
      if (index < 0 || this.rulesManager.RuleSets.Count < index)
      {
        this.SetButtons();
      }
      else
      {
        CApRulesSet ruleSet = this.rulesManager.RuleSets[index];
        this.listOfRules.Items.Clear();
        this.rules.Clear();
        foreach (RuleBase rule in ruleSet.Rules)
          this.rules.Add(rule);
        foreach (RuleBase rule in this.rules)
        {
          ListBoxItem newItem = new ListBoxItem();
          newItem.Content = (object) $"{this.listOfRules.Items.Count + 1}{"."} {rule.DisplayString}";
          newItem.MouseDoubleClick += new MouseButtonEventHandler(this.newItem_MouseDoubleClick);
          this.listOfRules.Items.Add((object) newItem);
        }
        this.UpdatePreview();
        this.SetButtons();
      }
    }
  }

  private void btnFilter_Checked(object sender, RoutedEventArgs e)
  {
    List<CApRenameElement> typesList = this.manager.GetTypesList();
    foreach (CApRenameElement capRenameElement in typesList)
    {
      if (this.filteredOutObjects.Contains<string>(capRenameElement.ObjectType))
        capRenameElement.IsChecked = false;
    }
    CUiRenameToolFilter renameToolFilter = new CUiRenameToolFilter(typesList);
    System.Windows.Point screen = this.btnFilter.PointToScreen(new System.Windows.Point(0.0, 0.0));
    ((Window) renameToolFilter).Left = screen.X / TxScaling.GetDPIScale();
    ((Window) renameToolFilter).Top = screen.Y / TxScaling.GetDPIScale() + this.btnFilter.ActualHeight;
    ((Window) renameToolFilter).Closed += new EventHandler(this.filterWindow_Closed);
    ((Window) renameToolFilter).Show();
  }

  private void filterWindow_Closed(object sender, EventArgs e)
  {
    CUiRenameToolFilter renameToolFilter = sender as CUiRenameToolFilter;
    ((Window) renameToolFilter).Closed -= new EventHandler(this.filterWindow_Closed);
    if (renameToolFilter.DialogResult)
    {
      this.filteredOutObjects = renameToolFilter.GetFilteredOutObjects();
      this.manager.FilterScopeObjects(this.filteredOutObjects);
      if (this.btnFilter.Content is StackPanel)
      {
        StackPanel content = this.btnFilter.Content as StackPanel;
        if (content.Children.Count == 2 && content.Children[1] is System.Windows.Controls.Image)
        {
          Uri uriSource = this.filteredOutObjects.Count <= 0 ? new Uri("pack://application:,,,/DnProcessSimulateCommands;component/RenameTool/Resources/filter_down.ico") : new Uri("pack://application:,,,/DnProcessSimulateCommands;component/RenameTool/Resources/filter_applied.ico");
          (content.Children[1] as System.Windows.Controls.Image).Source = (ImageSource) new BitmapImage(uriSource);
        }
      }
      this.UpdatePreview();
    }
    this.btnFilter.IsChecked = new bool?(false);
  }

  private void ResetFilter()
  {
    this.filteredOutObjects.Clear();
    if (!(this.btnFilter.Content is StackPanel))
      return;
    StackPanel content = this.btnFilter.Content as StackPanel;
    if (content.Children.Count != 2 || !(content.Children[1] is System.Windows.Controls.Image))
      return;
    Uri uriSource = new Uri("pack://application:,,,/DnProcessSimulateCommands;component/RenameTool/Resources/filter_down.ico");
    (content.Children[1] as System.Windows.Controls.Image).Source = (ImageSource) new BitmapImage(uriSource);
  }

  private void RenameWindow_Closing(object sender, CancelEventArgs e)
  {
    this.manager.ResetManager();
    CApDataElements.ItemWasChecked -= new D(this.CApDataElements_ItemWasChecked);
    this.m_flexGrid.SelectionChanged -= new SelectionChangedEventHandler(this.m_flexGrid_SelectionChanged);
  }

  private void ClearPreview()
  {
    foreach (CApRenameElement element in (Collection<CApRenameElement>) this.manager.Elements)
      element.Preview = element.Name;
    this.objectNames.Clear();
  }

  private void AddSelectedItems()
  {
    System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    try
    {
      this.CheckRuleMessageResult(this.manager.AddSelectedItems(), true);
    }
    finally
    {
      System.Windows.Forms.Cursor.Current = current;
    }
  }

  private void CheckRuleMessageResult(RuleMessage result, bool warning)
  {
    if (result.Success)
      return;
    int num = (int) TxMessageBox.Show(result.Message, warning ? XamlResourceManager.GetString("RENAME_TOOL_WARNING") : XamlResourceManager.GetString("RENAME_TOOL_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/renametool/cuirenametoolwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.RenameWindow = (CUiRenameToolWindow) target;
        ((Window) this.RenameWindow).Closing += new CancelEventHandler(this.RenameWindow_Closing);
        break;
      case 2:
        this.btApply = (System.Windows.Controls.Button) target;
        this.btApply.Click += new RoutedEventHandler(this.btApply_Click);
        break;
      case 3:
        this.button2 = (System.Windows.Controls.Button) target;
        this.button2.Click += new RoutedEventHandler(this.cancel_Click);
        break;
      case 4:
        this.cbScopeBox = (System.Windows.Controls.ComboBox) target;
        this.cbScopeBox.SelectionChanged += new SelectionChangedEventHandler(this.ScopeComboBox_SelectionChanged);
        break;
      case 5:
        this.cbi1 = (ComboBoxItem) target;
        break;
      case 6:
        this.cbi2 = (ComboBoxItem) target;
        break;
      case 7:
        this.cbi3 = (ComboBoxItem) target;
        break;
      case 8:
        this.btnFilter = (ToggleButton) target;
        this.btnFilter.Checked += new RoutedEventHandler(this.btnFilter_Checked);
        break;
      case 9:
        this.m_flexGrid = (System.Windows.Controls.DataGrid) target;
        this.m_flexGrid.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(this.m_flexGrid_PreviewKeyDown);
        break;
      case 10:
        this.btnSplitRemoveScope = (TxSplitButton) target;
        this.btnSplitRemoveScope.Click += new RoutedEventHandler(this.btRemove_Click);
        break;
      case 11:
        this.btAdd = (System.Windows.Controls.Button) target;
        this.btAdd.Click += new RoutedEventHandler(this.add_Click);
        break;
      case 12:
        this.btUp = (System.Windows.Controls.Button) target;
        this.btUp.Click += new RoutedEventHandler(this.btUp_Click);
        break;
      case 13:
        this.imgArrowUpTop = (System.Windows.Controls.Image) target;
        break;
      case 14:
        this.btDown = (System.Windows.Controls.Button) target;
        this.btDown.Click += new RoutedEventHandler(this.btDown_Click);
        break;
      case 15:
        this.imgArrowDownTop = (System.Windows.Controls.Image) target;
        break;
      case 16 /*0x10*/:
        this.listOfRules = (System.Windows.Controls.ListBox) target;
        this.listOfRules.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(this.listOfRules_PreviewKeyDown);
        this.listOfRules.SelectionChanged += new SelectionChangedEventHandler(this.listOfRules_SelectionChanged);
        break;
      case 17:
        this.btnSplitRemoveRule = (TxSplitButton) target;
        break;
      case 18:
        this.btnSplitAddRule = (TxSplitButton) target;
        break;
      case 19:
        this.btnEditRule = (System.Windows.Controls.Button) target;
        this.btnEditRule.Click += new RoutedEventHandler(this.btnEditRule_Click);
        break;
      case 20:
        this.btnRuleSet = (System.Windows.Controls.Button) target;
        this.btnRuleSet.Click += new RoutedEventHandler(this.btnEditRuleSet_Click);
        break;
      case 21:
        this.imgRule = (System.Windows.Controls.Image) target;
        break;
      case 22:
        this.btnSplitSave = (TxSplitButton) target;
        this.btnSplitSave.Click += new RoutedEventHandler(this.btnSaveRule_Click);
        break;
      case 23:
        this.cbRules = (System.Windows.Controls.ComboBox) target;
        this.cbRules.SelectionChanged += new SelectionChangedEventHandler(this.cbRules_SelectionChanged);
        break;
      case 24:
        this.btnRulesUp = (System.Windows.Controls.Button) target;
        this.btnRulesUp.Click += new RoutedEventHandler(this.btRulesUp_Click);
        break;
      case 25:
        this.imgArrowUpBottom = (System.Windows.Controls.Image) target;
        break;
      case 26:
        this.btnRulesDown = (System.Windows.Controls.Button) target;
        this.btnRulesDown.Click += new RoutedEventHandler(this.btRulesDown_Click);
        break;
      case 27:
        this.imgArrowDownBottom = (System.Windows.Controls.Image) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
